package com.shanghaionstar.remote.share;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.onstar.cn.api.UsersService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.smart.BaseResponse;
import com.onstar.cn.vehicle.response.AuthorizedSubscriberResponse;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by 11203rl on 2017/5/19.
 */

public class ShareRemoteService extends BaseService {
    private Context context;

    public ShareRemoteService(Context context) {
        this.context = context;
    }

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public Object jsonToBean(String json) {
        return null;
    }


    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public String getAuthorizedSubsJson() throws OnStarException {
        UsersService usersService = new UsersService(Constants.CURRENT_SERVER_URL);
        String subscriberId = ApplicationUtil.getInstance().getSubscriberID();
        String accountId = ApplicationUtil.getInstance().getAccountId();
        String vin = ApplicationUtil.getInstance().getVin();
        RequestEntity requestEntity = usersService.getAuthorizedSubs(new RequestEntity(),subscriberId,accountId,vin);
        addAuthorization(requestEntity);
        requestEntity = HttpInjectUtils.injectHttp(ShareRemoteService.this.getClass(), "getAuthorizedSubsJson", requestEntity);
        return BusinessUtil.getResponse(getDatas(requestEntity), context);
    }

    private void addAuthorization(RequestEntity requestEntity) {
        LinkedHashMap<String, Object> coHashMap = new LinkedHashMap<String, Object>();
        coHashMap.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(coHashMap);
    }

    public Observable<List<AuthorizedSubscriberResponse>> getAuthorizedSubs() {
        return Observable.defer(new Func0<Observable<List<AuthorizedSubscriberResponse>>>() {
            @Override
            public Observable<List<AuthorizedSubscriberResponse>> call() {
                try {
                    Type type = new TypeToken<ArrayList<AuthorizedSubscriberResponse>>() {}.getType();
                    List<AuthorizedSubscriberResponse> response = new Gson().fromJson(getAuthorizedSubsJson(), type);
                    return Observable.just(response);
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    //    /api/v3/user/subscriber/{subscriberId}/accounts/{accountId}/vehicles/{vin}/carSharing/status
    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public String getSharingStatusJson() throws OnStarException {
        UsersService usersService = new UsersService(Constants.CURRENT_SERVER_URL);
        String subscriberId = ApplicationUtil.getInstance().getSubscriberID();
        String accountId = ApplicationUtil.getInstance().getAccountId();
        String vin = ApplicationUtil.getInstance().getVin();
        RequestEntity requestEntity = usersService.getSharingStatus(new RequestEntity(),subscriberId,accountId,vin);
        addAuthorization(requestEntity);
        requestEntity = HttpInjectUtils.injectHttp(ShareRemoteService.this.getClass(), "getSharingStatusJson", requestEntity);
        return BusinessUtil.getResponse(getDatas(requestEntity), context);
    }

    public Observable<AuthorizedSubscriberResponse> getSharingStatus() {
        return Observable.defer(new Func0<Observable<AuthorizedSubscriberResponse>>() {
            @Override
            public Observable<AuthorizedSubscriberResponse> call() {
                try {
                    ArrayList<AuthorizedSubscriberResponse> list = new Gson().fromJson(getSharingStatusJson(), new TypeToken<ArrayList<AuthorizedSubscriberResponse>>(){}.getType());
                    if(list != null && !list.isEmpty())
                        return Observable.just(list.get(0));
                    else
                        return Observable.just(new AuthorizedSubscriberResponse());
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    //    /api/v3/user/subscriber/{subscriberId}/accounts/{accountId}/vehicles/{vin}/carSharing/authorization
    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public String putSharingAuthorizationJson(AuthorizedSubscriberResponse request) throws OnStarException {
        UsersService usersService = new UsersService(Constants.CURRENT_SERVER_URL);
        String subscriberId = ApplicationUtil.getInstance().getSubscriberID();
        String accountId = ApplicationUtil.getInstance().getAccountId();
        String vin = ApplicationUtil.getInstance().getVin();
        RequestEntity requestEntity = usersService.putSharingAuthorization(new RequestEntity(),subscriberId,accountId,vin);
        requestEntity.setBody(GsonHelper.toJson(request));
        addAuthorization(requestEntity);
        requestEntity = HttpInjectUtils.injectHttp(ShareRemoteService.this.getClass(), "putSharingAuthorizationJson", requestEntity,AuthorizedSubscriberResponse.class);
        return BusinessUtil.getResponse(getDatas(requestEntity), context);
    }

    public Observable<BaseResponse> putSharingAuthorization(final AuthorizedSubscriberResponse request) {
        return Observable.defer(new Func0<Observable<BaseResponse>>() {
            @Override
            public Observable<BaseResponse> call() {
                try {
                    return Observable.just(GsonHelper.fromJson(putSharingAuthorizationJson(request),BaseResponse.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

}
